package com.dandelion.framework.handler;


import cn.hutool.json.JSONUtil;
import com.dandelion.framework.common.R;
import com.dandelion.framework.common.util.WebUtils;
import com.dandelion.framework.enums.CodeEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 认证入口点impl
 *
 * @author 77088
 * @date 2022/02/16
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        R r = R.fail(CodeEnum.UNAUTHORIZED.getCode(), CodeEnum.UNAUTHORIZED.getMsg());
        //用户名密码错误会调用此方法
        WebUtils.renderString(response, JSONUtil.toJsonStr(r));
    }
}
